﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DAO;
using Microsoft.Phone.Controls;
using System.Windows.Threading;
using System.Windows.Media.Imaging;
using BUS;

namespace ProjectM2Work.Pages
{
    public partial class PageSchoolTopic : PhoneApplicationPage
    {
        static double SCAREWIDTH = 0f;
        static double SCAREHEIGHT = 0f;

        //dang bài giảng
        private int _type; //1 : truong hoc , 2: gia dinh ,3 : nau an
        
        //is click
        private int _typeClick;

        //
        LongTextblock tbChiTiet = new LongTextblock();

        //timer
        DispatcherTimer dt = new DispatcherTimer();
        DispatcherTimer currentPosition = new DispatcherTimer();
        bool isScale = false;
        public PageSchoolTopic()
        {
            InitializeComponent();
            SCAREWIDTH = System.Windows.Application.Current.Host.Content.ActualWidth / 480;
            SCAREHEIGHT = System.Windows.Application.Current.Host.Content.ActualHeight / 800;

            SetDefaultValueScreenChosen();
            _typeClick = -1;

            dt.Start();
            dt.Interval = new TimeSpan(0, 0, 0, 0, 1);
            dt.Tick += new EventHandler(dt_Tick);
            meVideo.MediaOpened += new RoutedEventHandler(myMediaElement_MediaOpened);

            meVideo.MediaEnded += new RoutedEventHandler(myMediaElement_MediaEnded);

            meVideo.CurrentStateChanged += new RoutedEventHandler(myMediaElement_CurrentStateChanged);

            currentPosition.Tick += new EventHandler(currentPosition_Tick);

            //texeblock
            tbChiTiet.FontSize = 22;
            tbChiTiet.Foreground = new SolidColorBrush(Colors.Black);
            Panel.Children.Add(tbChiTiet.MainElement);
            tbChiTiet.Visibility = Visibility.Collapsed;
        }

        //timer play
        void dt_Tick(object sender, EventArgs e)
        {
            switch (_typeClick)
            {
                case 1:
                    Run(imgTu1);
                    break;
                case 2:
                    Run(imgTu2);
                    break;
                case 3:
                    Run(imgTu3);
                    break;
                case 4:
                    Run(imgTu4);
                    break;
            }
        }

        static float DETAL = 2f;
        //run image
        public void Run(Image img)
        {
            Thickness thickness = new Thickness(0,0,0,0);
            if (img.Margin.Top > 141)
                thickness.Top = img.Margin.Top - DETAL;
            else
                if (img.Margin.Top < 133)
                    thickness.Top = img.Margin.Top + DETAL;
                else
                    thickness.Top = 137;

            if (img.Margin.Left > 15)
                thickness.Left = img.Margin.Left - DETAL;
            else
                if (img.Margin.Left < 5)
                    thickness.Left = img.Margin.Left + DETAL;
                else
                    thickness.Left = 12;

            img.Margin = thickness;

            if (thickness.Left == 12 && thickness.Top == 137)
            {
                _typeClick = -1;
            }
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            _type = Int32.Parse(NavigationContext.QueryString["Type"]);
            SetImageSource();
        }

        /// <summary>
        /// set image source of type of topic
        /// </summary>
        private void SetImageSource()
        {
            switch (_type)
            {
                case 1: 
                    imgTu1.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("../images/w_btn_cogiao.png", UriKind.Relative));
                    imgTu2.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("../images/w_btn_truonghoc.png", UriKind.Relative));
                    imgTu3.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("../images/w_btn_banbe.png", UriKind.Relative));
                    imgTu4.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("../images/w_btn_banhoc.png", UriKind.Relative));

                    imgTieuDe.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("../images/chude_truonghoc.png", UriKind.Relative));
                    break;
                case 2:
                    imgTu1.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("../images/btn_family_dad.png", UriKind.Relative));
                    imgTu2.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("../images/btn_family_mom.png", UriKind.Relative));
                    imgTu3.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("../images/btn_family_sister.png", UriKind.Relative));
                    imgTu4.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("../images/btn_family_brother.png", UriKind.Relative));

                    imgTieuDe.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("../images/chude_giadinh.png", UriKind.Relative));
                    break;
                case 3:
                    imgTu1.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("../images/btn_nauan_bep.png", UriKind.Relative));
                    imgTu2.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("../images/btn_nauan_pot.png", UriKind.Relative));
                    imgTu3.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("../images/btn_nauan_dua.png", UriKind.Relative));
                    imgTu4.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("../images/btn_nauan_chef.png", UriKind.Relative));

                    imgTieuDe.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("../images/chude_nauan.png", UriKind.Relative));
                    break;
            }
        }
        void myMediaElement_MediaEnded(object sender, RoutedEventArgs e)
        {
            meVideo.Stop();
        }

        void myMediaElement_MediaOpened(object sender, RoutedEventArgs e)
        {
            performanceProgressBar.Maximum = (int)meVideo.NaturalDuration.TimeSpan.TotalMilliseconds;
            meVideo.Play();
        }
        /// <summary>
        /// set default value of screen chosen
        /// </summary>
        private void SetDefaultValueScreenChosen()
        {
            VisibilityScreenChosen(Visibility.Visible);

            ////title
            //imgTieuDe.Margin = new Thickness(326 * SCAREWIDTH, 65 * SCAREHEIGHT, 0, 0);

            ////ảnh của 4 từ
            imgTu1.Margin = new Thickness(230 * SCAREWIDTH, 79 * SCAREHEIGHT, 0, 0);
            imgTu2.Margin = new Thickness(429 * SCAREWIDTH, 82 * SCAREHEIGHT, 0, 0);
            imgTu3.Margin = new Thickness(580 * SCAREWIDTH, 230 * SCAREHEIGHT, 0, 0);
            imgTu4.Margin = new Thickness(312 * SCAREWIDTH, 244 * SCAREHEIGHT, 0, 0);

            VisibilityScreenPlayVideo(Visibility.Collapsed);
        }

        private void VisibilityScreenChosen(Visibility vis)
        {
            imgTu1.Visibility = vis;
            imgTu2.Visibility = vis;
            imgTu3.Visibility = vis;
            imgTu4.Visibility = vis;
        }

        /// <summary>
        /// set default value of screen play video
        /// </summary>
        private void SetDefaultValueScreenPlayVideo(int _typeClick)
        {
            TuVungBUS tuVungBus = new TuVungBUS();
            TuDien tudien = null;
            VisibilityScreenPlayVideo(Visibility.Visible);
                                    if (PublicAndStatic.HasInternetConection())
                        {
            switch (_typeClick)
            {
                case 1:
                    if (_type == 1)
                    {
                        tudien = tuVungBus.LayTuVungTheoID(347);
                        }
                    if (_type == 2)
                    {
                        tudien = tuVungBus.LayTuVungTheoID(120);
                    }
                    if (_type == 3)
                    {
                        tudien = tuVungBus.LayTuVungTheoID(1837); // bep
                        
                    }

                    break;
                case 2:
                    if (_type == 1)
                    {
                        tudien = tuVungBus.LayTuVungTheoID(1712);
                    }
                    if (_type == 2)
                    {
                        tudien = tuVungBus.LayTuVungTheoID(1122);
                    }
                    if (_type == 3)
                    {
                        tudien = tuVungBus.LayTuVungTheoID(1317);// cai noi
                    }
                    break;
                case 3:
                      if (_type == 1)
                    {
                        tudien = tuVungBus.LayTuVungTheoID(2099);
                    }
                    if (_type == 2)
                    {
                        tudien = tuVungBus.LayTuVungTheoID(279);
                    }
                    if (_type == 3)
                    {
                        tudien = tuVungBus.LayTuVungTheoID(533); //dua
                    }
                    break;
                case 4:
                     if (_type == 1)
                    {
                        tudien = tuVungBus.LayTuVungTheoID(49);
                    }
                    if (_type == 2)
                    {

                        tudien = tuVungBus.LayTuVungTheoID(21);
                    }
                    if (_type == 3)
                    {
                        tudien = tuVungBus.LayTuVungTheoID(2409); // cai thot
                    }
                    break;
            }
                            meVideo.Source = PublicAndStatic.ConvertToUri(tudien);
            tbChiTiet.Text = "Mô tả: \n" + tudien.Description;
            meVideo.Position = new TimeSpan(0, 0, 0, 0);
            meVideo.Play();
                                    }
                                                                else
                        {
                            MessageBox.Show("Không có kết nối Internet.!");
                            return;
                        }
                                    
            //imgTV.Margin = new Thickness(11 * SCAREWIDTH, 5 * SCAREHEIGHT, 0, 0);
            //imgBGDetail.Margin = new Thickness(11 * SCAREWIDTH, 5 * SCAREHEIGHT, 0, 0);
            VisibilityScreenChosen(Visibility.Collapsed);
        }

        private void VisibilityScreenPlayVideo(Visibility vis)
        {
            //disable screen play video
            imgTV.Visibility = vis;
            imgBGDetail.Visibility = vis;
            meVideo.Visibility = vis;
            imgBack.Visibility = vis;

            imgPlay.Visibility = vis;
            imgPause.Visibility = vis;
            imgStop.Visibility = vis;
            tbChiTiet.Visibility = vis;
            performanceProgressBar.Visibility = vis;
            if (vis == Visibility.Collapsed)
                tbChiTiet.Text = "";
            
        }
        void myMediaElement_CurrentStateChanged(object sender, RoutedEventArgs e)
        {

            if (meVideo.CurrentState == MediaElementState.Playing)
            {

                currentPosition.Start();
                BitmapImage tn = new BitmapImage();
                tn.SetSource(Application.GetResourceStream(new Uri(@"/ProjectM2Work;component/images/btn_play_disable.png", UriKind.Relative)).Stream);
                imgPlay.Source = tn;

                tn = new BitmapImage();
                tn.SetSource(Application.GetResourceStream(new Uri(@"/ProjectM2Work;component/images/btn_pause_enable.png", UriKind.Relative)).Stream);
                imgPause.Source = tn;

                tn = new BitmapImage();
                tn.SetSource(Application.GetResourceStream(new Uri(@"/ProjectM2Work;component/images/btn_stop_enable.png", UriKind.Relative)).Stream);
                imgStop.Source = tn;

            }

            else if (meVideo.CurrentState == MediaElementState.Paused)
            {

                currentPosition.Stop();
                BitmapImage tn = new BitmapImage();
                tn.SetSource(Application.GetResourceStream(new Uri(@"/ProjectM2Work;component/images/btn_play_enable.png", UriKind.Relative)).Stream);
                imgPlay.Source = tn;

                tn = new BitmapImage();
                tn.SetSource(Application.GetResourceStream(new Uri(@"/ProjectM2Work;component/images/btn_pause_disable.png", UriKind.Relative)).Stream);
                imgPause.Source = tn;

                tn = new BitmapImage();
                tn.SetSource(Application.GetResourceStream(new Uri(@"/ProjectM2Work;component/images/btn_stop_enable.png", UriKind.Relative)).Stream);
                imgStop.Source = tn;


            }

            else
            {

                currentPosition.Stop();


                currentPosition.Stop();
                BitmapImage tn = new BitmapImage();
                tn.SetSource(Application.GetResourceStream(new Uri(@"/ProjectM2Work;component/images/btn_play_enable.png", UriKind.Relative)).Stream);
                imgPlay.Source = tn;

                tn = new BitmapImage();
                tn.SetSource(Application.GetResourceStream(new Uri(@"/ProjectM2Work;component/images/btn_pause_disable.png", UriKind.Relative)).Stream);
                imgPause.Source = tn;

                tn = new BitmapImage();
                tn.SetSource(Application.GetResourceStream(new Uri(@"/ProjectM2Work;component/images/btn_stop_disable.png", UriKind.Relative)).Stream);
                imgStop.Source = tn;

            }
        }
        void currentPosition_Tick(object sender, EventArgs e)
        {
            performanceProgressBar.Value = (int)meVideo.Position.TotalMilliseconds;
        }
        private void imgTu1_MouseMove(object sender, MouseEventArgs e)
        {
            if (isScale == false)
            {
                imgTu1.Height = imgTu1.Height * 1.1f;
                imgTu1.Width = imgTu1.Width * 1.1f;
                isScale = true;
            }
        }

        private void imgTu1_MouseLeave(object sender, MouseEventArgs e)
        {
            if (isScale == true)
            {
                imgTu1.Height = imgTu1.Height / 1.1f;
                imgTu1.Width = imgTu1.Width / 1.1f;
                isScale = false;
            }
        }

        private void imgTu1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (isScale == false)
            {
                imgTu1.Height = imgTu1.Height * 1.1f;
                imgTu1.Width = imgTu1.Width * 1.1f;
                isScale = true;
            }
        }

        private void imgTu2_MouseLeave(object sender, MouseEventArgs e)
        {
            if (isScale == true)
            {
                imgTu2.Height = imgTu2.Height / 1.1f;
                imgTu2.Width = imgTu2.Width / 1.1f;
                isScale = false;
            }
        }

        private void imgTu2_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (isScale == false)
            {
                imgTu2.Height = imgTu2.Height * 1.1f;
                imgTu2.Width = imgTu2.Width * 1.1f;
                isScale = true;
            }
        }

        private void imgTu2_MouseMove(object sender, MouseEventArgs e)
        {
            if (isScale == false)
            {
                imgTu2.Height = imgTu2.Height * 1.1f;
                imgTu2.Width = imgTu2.Width * 1.1f;
                isScale = true;
            }
        }

        private void imgTu4_MouseMove(object sender, MouseEventArgs e)
        {
            if (isScale == false)
            {
                imgTu4.Height = imgTu4.Height * 1.1f;
                imgTu4.Width = imgTu4.Width * 1.1f;
                isScale = true;
            }
        }

        private void imgTu4_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (isScale == false)
            {
                imgTu4.Height = imgTu4.Height * 1.1f;
                imgTu4.Width = imgTu4.Width * 1.1f;
                isScale = true;
            }
        }

        private void imgTu4_MouseLeave(object sender, MouseEventArgs e)
        {
            if (isScale == true)
            {
                imgTu4.Height = imgTu4.Height / 1.1f;
                imgTu4.Width = imgTu4.Width / 1.1f;
                isScale = false;
            }
        }

        private void imgTu3_MouseMove(object sender, MouseEventArgs e)
        {
            if (isScale == false)
            {
                imgTu3.Height = imgTu3.Height * 1.1f;
                imgTu3.Width = imgTu3.Width * 1.1f;
                isScale = true;
            }
        }

        private void imgTu3_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (isScale == false)
            {
                imgTu3.Height = imgTu3.Height * 1.1f;
                imgTu3.Width = imgTu3.Width * 1.1f;
                isScale = true;
            }
        }

        private void imgTu3_MouseLeave(object sender, MouseEventArgs e)
        {
            if (isScale == true)
            {
                imgTu3.Height = imgTu3.Height / 1.1f;
                imgTu3.Width = imgTu3.Width / 1.1f;
                isScale = false;
            }
        }

        private void imgChuDe_MouseMove(object sender, MouseEventArgs e)
        {
            if (isScale == false)
            {
                imgChuDe.Height = imgChuDe.Height * 1.1f;
                imgChuDe.Width = imgChuDe.Width * 1.1f;
                isScale = true;
            }
        }

        private void imgChuDe_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (isScale == false)
            {
                imgChuDe.Height = imgChuDe.Height * 1.1f;
                imgChuDe.Width = imgChuDe.Width * 1.1f;
                isScale = true;
            }
        }

        private void imgChuDe_MouseLeave(object sender, MouseEventArgs e)
        {
            if (isScale == true)
            {
                imgChuDe.Height = imgChuDe.Height / 1.1f;
                imgChuDe.Width = imgChuDe.Width / 1.1f;
                isScale = false;
            }
        }

        private void imgTu1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            _typeClick = 1;
            DETAL = 2f;
            SetDefaultValueScreenPlayVideo(_typeClick);
            imgTu1.Visibility = System.Windows.Visibility.Visible;
        }

        private void imgTu2_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            _typeClick = 2;
            DETAL = 5f;
            SetDefaultValueScreenPlayVideo(_typeClick);
            imgTu2.Visibility = System.Windows.Visibility.Visible;
        }

        private void imgTu3_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            _typeClick = 3;
            DETAL = 7f;
            SetDefaultValueScreenPlayVideo(_typeClick);
            imgTu3.Visibility = System.Windows.Visibility.Visible;
        }

        private void imgTu4_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            _typeClick = 4;
            DETAL = 10f;
            SetDefaultValueScreenPlayVideo(_typeClick);
            imgTu4.Visibility = System.Windows.Visibility.Visible;
        }

        private void imgBack_MouseLeave(object sender, MouseEventArgs e)
        {
            if (isScale == true)
            {
                imgBack.Height = imgBack.Height / 1.1f;
                imgBack.Width = imgBack.Width / 1.1f;
                isScale = false;
            }
        }

        private void imgBack_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (isScale == false)
            {
                imgBack.Height = imgBack.Height * 1.1f;
                imgBack.Width = imgBack.Width * 1.1f;
                isScale = true;
            }
        }

        private void imgBack_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            _typeClick = -1;
            SetDefaultValueScreenChosen();
        }

        private void imgBack_MouseMove(object sender, MouseEventArgs e)
        {
            if (isScale == true)
            {
                imgBack.Height = imgBack.Height / 1.1f;
                imgBack.Width = imgBack.Width / 1.1f;
                isScale = false;
            }
        }

        private void imgPlay_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            meVideo.Play();
        }

        private void imgPause_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            meVideo.Pause();
        }

        private void imgStop_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            meVideo.Stop();
        }

        private void imgChuDe_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Pages/PageChuong02.xaml", UriKind.Relative));
        }


    }
}